{extend name='public/content'}

{block name="title"}
	购物车
{/block}

{block name="body"}
<div>
	<div>地址</div>	
	{foreach(ordecar as $key=>$vo)}
		<div>
			<span>门店：{$vo.store.title}</span><br/>
			<span>商品名称:{$vo.goods.name}</span>
			<span><img style="width: 100px;height: 100px" src="{$vo.goods.logo}"></span>
			<span>商品名称:{$vo.goods.name}</span>
			<span>商品价格:{$vo.goods_price}</span>
			<span>商品数量:{$vo.goods_sum}</span>
			<span>商品总价:{$vo.goods_sum_price}</span>

			<input type="checkbox" class="inputcheck layui-bg-green" 
					name="check[{$key}]"  data-name="{$vo.goods.name}" 
					data-storeid="{$vo.storeid}" value="{$vo.id}">	
			<a href="{:url('order/deletecar')}?id={$vo.id}">
				<button class="deletgouwuce layui-btn layui-btn-radius layui-btn-normal" data-id="{$vo.id}">删除</button>
			</a>	
		</div>

	{/foreach}
	<input type="text" name="dress">
	<button class="lijxiagn">立即下单</button>
</div>	
{/block}

{block name="script"}
 	<script type="text/javascript">
 		var url={
 			adrelist:"{:url('index/user/ajaxaddreslist')}",	
 			addorderbycar:"{:url('index/order/addorderbycar')}",
 			orderpay:"{:url('index/order/orderpay')}"
 		}
 		require(['jquery'], function () {
 			/**
 				获取地址
 				**/
 			$.ajax({
 		 		url: url.adrelist,
 		 		type: 'GET',
 		 		dataType: 'json',
 		 		data: {},
 		 	})
 		 	.done(function(data) {
 		 		for (var i = data.length - 1; i >= 0; i--) {
 		 			var id =data[i].id;
 		 			if(i==0){
 		 				$('input[name=dress]').val(id);
 		 			}
 		 		}
 		 	})
 		 	.fail(function() {
 		 		console.log("error");
 		 	})
 		 	.always(function() {
 		 		
 		 	});
 		 	 var  storeid = '';
 		 	$(".inputcheck").click(function(){
 		 		let inputcheck =document.querySelectorAll(".inputcheck");
 		 		console.log(inputcheck);
 		 		let chectisy= 0;
 		 		for (var i = inputcheck.length - 1; i >= 0; i--) {
 		 			if(inputcheck[i].checked==true){
 		 				chectisy =1;
 		 			}
 		 		}
 		 		if(chectisy==0){
 		 			alert("没选中任何商品");
 		 			return;
 		 		}
 		 		let strid = $(this).data("storeid");
 		 		if(storeid==''){
 		 			storeid = strid
 		 		}
 		 		if(strid!=storeid){
 		 			alert("不是同一个门店的不能选");
 		 			$(this)[0].checked =false;
 		 			return ;
 		 		}
 		 	})

 			$('.lijxiagn').click(function(event) {
 				let chec=[];
 				let storeid= '';
 				$('.inputcheck').each(function(index, el) {
 					console.dir(el);
 					if($(el).is(':checked')) {
 						storeid = $(el).data("storeid")
						chec.push($(el).val());
						console.dir(el);
						return el;
					}
 				});
 				let idcar= chec.join(',');
 				if(chec.length==0){
 					alert("请选择商品");
 					return;
 				}
 				let data = {
 					ordercarids : idcar,
 					dress	:$('input[name=dress]').val(),
 					storeid
 				}
 				$.ajax({
 					url: url.addorderbycar,
 					type: 'POST',
 					dataType: 'json',
 					data:data,
 				})
 				.done(function(data) {
 					if(data.status==0){
 						alert(data.msg);
 						return ;
 					}else{
 						alert(data.data);
 						location.href=url.orderpay+"?id="+data.data;
 					}
 					
 					
 				})
 				.fail(function() {
 					//console.log("error");
 				})
 				.always(function() {
 					//console.log("complete");
 				});
 				



 			});
 		})
 	</script>	
{/block}

{block name="style"}
 <style type="text/css">
 	
 </style>
{/block}